package dao

import (
	"fmt"
	"github.com/garyburd/redigo/redis"
	"github.com/wonderivan/logger"
	"ucenter/app"
)

//TODO 连接池配置
/**
 * 设置游戏服的登录token
 * param: string key
 * param: string val
 */
func RedisLoginSet(key string, val string) {
	//写死 后期优化到配置文件 TODO
	c, err := redis.Dial("tcp", app.Conf.LoginRedis.Addr,
		redis.DialPassword(app.Conf.LoginRedis.Auth), redis.DialDatabase(app.Conf.LoginRedis.Database))
	if err != nil {
		logger.Error("Redis_Login_Set_Init_Error | err=%v", err)
		fmt.Println("Connect to redis error", err)
		return
	}
	defer c.Close()

	_, err = c.Do("SET", key, val, "EX", "86400")
	if err != nil {
		logger.Error("Redis_Login_Set_Failed | err=%v", err)
		fmt.Println("redis set failed:", err)
		return

	}
}

func RedisSet(key string, val string) {
	fmt.Println(app.Conf.Redis.Auth)
	fmt.Println(app.Conf.Redis.Addr)
	c, err := redis.Dial("tcp", app.Conf.Redis.Addr,
		redis.DialPassword(app.Conf.Redis.Auth), redis.DialDatabase(app.Conf.Redis.Database))
	fmt.Println(err)
	fmt.Println(111111111)
	if err != nil {
		logger.Error("Redis_Set_Init_Error | err=%v", err)
		fmt.Println("Connect to redis error", err)
		return
	}
	defer c.Close()

	row, err := c.Do("SET", key, val, "EX", "86400")
	fmt.Println(row)
	if err != nil {
		logger.Error("Redis_Set_Failed | err=%v", err)
		fmt.Println("redis set failed:", err)
		return
	}
}

func RedisGet(key string) interface{} {
	c, err := redis.Dial("tcp", app.Conf.Redis.Addr,
		redis.DialPassword(app.Conf.Redis.Auth), redis.DialDatabase(app.Conf.Redis.Database))
	if err != nil {
		logger.Error("Redis_Set_Init_Error | err=%v", err)
		fmt.Println("Connect to redis error", err)
		return nil
	}
	defer c.Close()

	val, err := c.Do("GET", key)
	if err != nil {
		logger.Error("Redis_Set_Failed | err=%v", err)
		fmt.Println("redis set failed:", err)
		return nil
	}
	return val
}
